package com.example.homecunter.video.repository;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.example.common.utils.BaseEntity;
import com.example.homecunter.video.entity.ChannelEntity;
import com.example.homecunter.video.entity.VideoEntity;
import com.example.homecunter.video.model.ChannelModel;
import com.example.mvvmcore.repository.BaseRepository;

import org.reactivestreams.Subscription;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.FlowableSubscriber;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
/**
 * 数据缓存区
 */
public class ChannelRepository extends BaseRepository<ChannelModel> {
    @Override
    protected ChannelModel createModel() {
        return new ChannelModel();
    }


    public MutableLiveData<BaseEntity<List<ChannelEntity>>> channel(){

        MutableLiveData<BaseEntity<List<ChannelEntity>>> liveData = new MutableLiveData<>();

        Flowable<BaseEntity<List<ChannelEntity>>> channel = mModel.channel();

        channel.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new FlowableSubscriber<BaseEntity<List<ChannelEntity>>>() {

                    @Override
                    public void onSubscribe(Subscription s) {
                        s.request(Integer.MAX_VALUE);
                    }
                    @Override
                    public void onNext(BaseEntity<List<ChannelEntity>> channelEntityBaseEntity) {
                        //背压
//                        if (Looper.getMainLooper().getThread()==Thread.currentThread()){
//                            liveData.setValue(channelEntityBaseEntity);
//                        }else{
//                            liveData.postValue(channelEntityBaseEntity);
//                        }
                        liveData.setValue(channelEntityBaseEntity);
                    }
                    @Override
                    public void onError(Throwable t) {

                    }
                    @Override
                    public void onComplete() {

                    }
                });
        return liveData;
    }


    public MutableLiveData<BaseEntity<List<VideoEntity>>> videoData(int channelid,int page){

        MutableLiveData<BaseEntity<List<VideoEntity>>> videoData = new MutableLiveData<>();

        Flowable<BaseEntity<List<VideoEntity>>> video = mModel.video(channelid,page);

        video.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new FlowableSubscriber<BaseEntity<List<VideoEntity>>>() {
                    @Override
                    public void onSubscribe(Subscription s) {
                        s.request(Integer.MAX_VALUE);
                    }

                    @Override
                    public void onNext(BaseEntity<List<VideoEntity>> listBaseEntity) {

                        Log.i("--a", "onNext: "+listBaseEntity.getData());
                        videoData.setValue(listBaseEntity);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return videoData;
    }


}
